package xkp.lesson4;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import xkp.lesson4.utils.UrlUtils;

import java.io.IOException;
import java.util.logging.Level;

public class TestBossJsoup {
    public static void main(String[] args) throws IOException {
      /*  Document document = Jsoup.connect("https://www.zhipin.com/c101010100/?query=java&page=2&ka=page-2").get();
        System.out.println(document.html());*/
        //String url = "https://www.zhipin.com/" + cityNum + "?query=" + kw + "&page=" + page + "&ka=page-" + page;
       // String url = "https://www.zhipin.com/c101010100/?query=java&page=2&ka=page-2";

        //System.out.println("----------------");
        String[] cityCodes = {"c101010100","101020100","101280100","101280600","101210100","101030100","101110100","101190400",
            "101200100","101230200","101250100","101270100","101180100"
        };

        //5*13*14*30
        String[] keyWords = {"java","前端","c语言","嵌入式","Android"};
        for(String cityCode:cityCodes){
            for(String keyWord:keyWords){
                readJobData(cityCode,keyWord,1);
            }
        }

        //readJobData("c101010100","java",10);
    }


    public static void readJobData(String cityCode,String keyWord,int pageNum) throws IOException {
        String url = UrlUtils.generateUrl(cityCode,keyWord,pageNum);
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        //屏蔽日志信息
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        //支持JavaScript
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setActiveXNative(false);
        // webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(5000);

        System.out.println(url);
        HtmlPage rootPage = webClient.getPage(url);
        //设置一个运行JavaScript的时间
        webClient.waitForBackgroundJavaScript(5000);
        String html = rootPage.asXml();

        //
        Document document = Jsoup.parse(html);
        //System.out.println(document.html());

        // Elements allJobli = document.select(".job-list>ul>li");
        Elements allJob = document.select(".job-primary");
        if(allJob == null|| allJob.size() == 0){
            return;
        }
        //System.out.println(allJob.size());
        for(Element thisJob:allJob){
            Element jobBoxElement = thisJob.selectFirst(".primary-box");
            String jobId = jobBoxElement.attr("data-jobid");
            Element jobNameElement = thisJob.selectFirst(".job-name>a");
            Element jobAreaElement = thisJob.selectFirst(".job-area");
            Element jobCompanyElement = thisJob.selectFirst(".company-text>.name>a");
            Element jobCompanyDesc = thisJob.selectFirst(".company-text>p");
            String[] descArr = jobCompanyDesc.text().split(" ");
            Element jobSalaryElement = thisJob.selectFirst(".job-limit>span");
            Element jobSalaryLmit = thisJob.selectFirst(".job-limit>p");
            //System.out.println(jobNameElement.attr("href"));
            //System.out.println(jobSalaryLmit.text());
            String[] limitArr = jobSalaryLmit.text().split(" ");
            System.out.println(jobNameElement.text()+"-->"+jobAreaElement.text()+"-->"+jobCompanyElement.text()+"-->"+jobSalaryElement.text());
        }
        pageNum++;
        readJobData(cityCode,keyWord,pageNum);
    }
}
